package com.qf.day09.work3;

import java.util.Arrays;
import java.util.Random;

public class Manage {
    public Fightable game(Fightable h1 , Fightable h2){
        if(h1 instanceof  Hero && h2 instanceof  Hero){
            while (true){
                if(h1.isAlive()){
                    h1.fight(h2);
                    h1.takeDamage(((Hero) h2).getHarm());
                    if(!h1.isAlive()){
                        System.out.println("胜者： "+h2);
                        return h2;
                    }
//                    System.out.println(h1);
                }
                if(h2.isAlive()){
                    h2.fight(h1);
                    h2.takeDamage(((Hero) h1).getHarm());
                    if(!h2.isAlive()){
                        System.out.println("胜者："+h1);
                        return h1;
                    }
//                    System.out.println(h2);
                }
            }
        }
        return null;
    }

    public static void teamPK(Fightable[] team1 ,Fightable[] team2){
        Manage manage = new Manage();
            int i=0;
            int j=0;
            while (i<3 && j<3){
                Fightable gamed = manage.game(team1[i], team2[j]);
                if(gamed == team1[i]){
                    j++;
                }else {
                    i++;
                }
            }
    }

    public static void main(String[] args) {
        Manage manage = new Manage();
//        Fightable[] arr1 = new Fightable[3];
//        Fightable[] arr2 = new Fightable[3];

        Fightable[] arr1 = new Hero[3];
        Fightable[] arr2 = new Hero[3];
        Hero hero1 = new Hero("李四",122);
        Hero hero2 = new Hero("王五",101);
        Hero hero3 = new Hero("张三",110);
       arr1[0] = hero1;
       arr1[1] = hero2;
       arr1[2] = hero3;

        Hero hero4 = new Hero("小李",100);
        Hero hero5 = new Hero("小王",110);
        Hero hero6 = new Hero("小张",120);
        arr2[0] = hero4;
        arr2[1] = hero5;
        arr2[2] = hero6;

        System.out.println("=========================================");
        HeroComparator comparator = new HeroComparator();


        Hero[] heroes = new Hero[arr1.length];
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] instanceof Hero) {
                heroes[i] = (Hero) arr1[i];
            } else {
                System.out.println("类型不匹配");
            }
        }
        Arrays.sort(heroes,comparator);

        for (Fightable a1 : heroes) {
            System.out.println(a1);
        }
        Manage.teamPK(arr1,arr2);
        Hero hero = new Hero("张三",100);
        Hero hero7 = new Hero("张三",100);
        System.out.println("===========================");
        System.out.println(hero);
        System.out.println(hero7);
        System.out.println(hero.equals(hero7));
    }
}
